package com.mvprxjavartrofit.ui.login;

import android.util.Log;

import com.mvprxjavartrofit.base.RxSchedulers;
import com.mvprxjavartrofit.network.HttpUtil;
import com.mvprxjavartrofit.entity.HttpResult;
import com.mvprxjavartrofit.entity.User;

import rx.Subscriber;
import rx.Subscription;

/**
 * Created by Administrator
 * At 2016/10/20/9:56/2016
 * model 抽离出Model不仅各层更加分工明确便于Model的复用，
 * 而且大大简化了Presenter的代码量，让P层更简洁更专注。职能清爽，一目了然。
 */
public class LoginModel implements LoginContract.Model {


    @Override
    public Subscription login(final LoginListener loginListener) {
        return HttpUtil.getInstance().getLoginApi()
                .login()
                .compose(RxSchedulers.<HttpResult<User>>ioMain())
                .subscribe(new Subscriber<HttpResult<User>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        loginListener.showMessage("网络连接失败");
                    }

                    @Override
                    public void onNext(HttpResult<User> result) {

                        Log.d("","*****result****"+result.toString());

                        if(result.getRespCode() != 0){
                            loginListener.showMessage(result.getRespMessage());
                            return;
                        }
                        //保存数据
                        Log.d("111", ((User)result.getData()).toString());
                        loginListener.showSucc();
                    }
                });
    }
}
